package com.dy.design_pattern_study.composite_pattern.dome1.impl;

import com.dy.design_pattern_study.composite_pattern.dome1.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @CreateBy zhangdingyi
 * @Date 2023-04-12
 * @Content 定义有子节点的行为，用来存储子部件，
 * Component 接口中实现与子部件有关的操作：add 和 remove
 */
public class Composite extends Component {
    /**
     * 子对象，用来存放其下属的子节点和叶节点
     */
    private List<Component> children = new ArrayList<>();

    public Composite(String name) {
        super(name);
    }

    @Override
    public void add(Component component) {
        children.add(component);
    }

    @Override
    public void remove(Component component) {
        children.remove(component);
    }

    /**
     * 用来展示其子节点名称并显示器下属子节点
     * @param depth
     */
    @Override
    public void display(int depth) {

        System.out.println(printStr(depth));

        for (Component component : children) {
            component.display(depth + 2);
        }
    }
}
